import { createRouter, createWebHistory } from 'vue-router'
import Dashboard from '../views/Dashboard.vue'
import Residents from '../views/Residents.vue'
import Staff from '../views/Staff.vue'
import Medical from '../views/Medical.vue'
import Activities from '../views/Activities.vue'
import Dining from '../views/Dining.vue'
import Facility from '../views/Facility.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/dashboard'
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: Dashboard,
      meta: {
        title: '首页'
      }
    },
    {
      path: '/residents',
      name: 'residents',
      component: Residents,
      meta: {
        title: '住户管理'
      }
    },
    {
      path: '/staff',
      name: 'staff',
      component: Staff,
      meta: {
        title: '员工管理'
      }
    },
    {
      path: '/medical',
      name: 'medical',
      component: Medical,
      meta: {
        title: '医疗记录'
      }
    },
    {
      path: '/activities',
      name: 'activities',
      component: Activities,
      meta: {
        title: '活动管理'
      }
    },
    {
      path: '/dining',
      name: 'dining',
      component: Dining,
      meta: {
        title: '餐饮管理'
      }
    },
    {
      path: '/facility',
      name: 'facility',
      component: Facility,
      meta: {
        title: '设施维护'
      }
    },
    {
      // 捕获所有未匹配的路由
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFound.vue'),
      meta: {
        title: '页面未找到'
      }
    }
  ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 养老院管理系统` : '养老院管理系统'
  
  // 这里可以添加其他路由守卫逻辑，比如：
  // - 权限检查
  // - 登录状态验证
  // - 路由跳转前的确认
  
  next()
})

// 路由错误处理
router.onError((error) => {
  console.error('路由错误:', error)
  // 可以在这里添加错误处理逻辑
})

export default router